package com.itheima.reggie.contronller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.LoginCode;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeContronller {
    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest req, @RequestBody Employee employee) {

        if (employee == null || StringUtils.isBlank(employee.getUsername()) || StringUtils.isBlank(employee.getPassword())) {
            return R.error("用户名和密码不能为空");
        }

        //    ①. 将页面提交的密码password进行md5加密处理, 得到加密后的字符串
        String password = employee.getPassword();
        String md5PassWord = DigestUtils.md5DigestAsHex(password.getBytes());
//②. 根据页面提交的用户名username查询数据库中员工数据信息

        Employee one = employeeService.getOne(new LambdaQueryWrapper<Employee>().eq(Employee::getUsername, employee.getUsername()));
        //③. 如果没有查询到, 则返回登录失败结果
        if (one == null) {
            return R.error("该用户名不存在");
        }


//④. 密码比对，如果不一致, 则返回登录失败结果
        if (!md5PassWord.equals(one.getPassword())) {
            return R.error("密码错误");
        }


//⑤. 查看员工状态，如果为已禁用状态，则返回员工已禁用结果
        if (one.getStatus().equals(0)) {
            return R.error("该用户已禁用");
        }
//⑥. 登录成功，将员工id存入Session, 并返回登录成功结果

        req.getSession().setAttribute(LoginCode.EMP_CHECKID, one.getId());
        return R.success(one);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest req) {
        req.getSession().removeAttribute(LoginCode.EMP_CHECKID);
        return R.success("退出成功");
    }

    @PostMapping
    public R<String> saveEmp(HttpServletRequest req,@RequestBody Employee employee) {
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
//
//        long id = (long) req.getSession().getAttribute(LoginCode.CheckId);
//        employee.setCreateUser(id);
//        employee.setUpdateUser(id);
        employeeService.save(employee);
        log.info("saveEmp运行了....");

        return R.success("添加用户成功");
    }


    @GetMapping("/page")
    public R<Page> pageEmp(Integer page,Integer pageSize,String name){
        Page<Employee> page_Emp = new Page<>(page,pageSize);

        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        employeeService.page(page_Emp,lambdaQueryWrapper);
        return R.success(page_Emp);
    }


    @PutMapping
    public R<String> updateEmp(HttpServletRequest request,@RequestBody Employee employee ){
         log.info(employee.getId().toString());
        log.info("--->{}",employee.getId().equals(1L));
        if (employee.getId().equals(1L) && employee.getStatus().equals(0)){
            return R.error("此账号不可以被禁用");
        }
        log.info("updateEmp运行了");
//        employee.setUpdateTime(LocalDateTime.now());
//        Long attribute =(long) request.getSession().getAttribute(LoginCode.CheckId);
//        employee.setUpdateUser(attribute);
        employee.setUpdateUser(null);
        employee.setUpdateTime(null);
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    @GetMapping("/{id}")
    public R<Employee> getByID(@PathVariable Long id){
        Employee employee = employeeService.getById(id);
        if (employee == null){
            return R.error("没有查询到该用户信息");
        }
        log.info("getById运行了...{}",id);
        return R.success(employee);

    }
}
